﻿using System;
using System.Web;
using System.Web.SessionState;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.Serialization.Json;
using WF.Common;
using WF.BusinessObject;

namespace WF.Web.Pages
{
    /// <summary>
    /// HandlerEmpLimite 的摘要说明
    /// </summary>
    public class HandlerEmpLimit : HrBasePage, IHttpHandler, IRequiresSessionState
    {

        public override void ProcessRequest(HttpContext context)
        {
            if (base.Valid)
            {
                DataContractJsonSerializer serializer = null;
                SecurityManager sm = null;
                var data = new List<vUserLimit>();
                var userCode = new ConfigHelper().EmployeeLimite.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                switch (context.Request["args"])
                {
                    //加载
                    case "load":
                        if (userCode.Length > 0)
                        {
                            sm = new SecurityManager();
                            data = sm.GetLimiteUsers(userCode);
                            serializer = new DataContractJsonSerializer(data.GetType());
                        }
                        break;
                    //查询
                    case "query":
                        sm = new SecurityManager();
                        data = (from a in sm.GetLimiteUsers(new string[] { context.Request["conds"] })
                                select new vUserLimit()
                                {
                                    Name = a.Name,
                                    Code = a.Code,
                                    DomainName = a.DomainName,
                                    Status = userCode.Contains(a.Code) ? "已开通" : "未开通"
                                }).ToList();
                        serializer = new DataContractJsonSerializer(data.GetType());
                        break;
                    case "set":
                        if (context.Request["flag"] == "true")
                            new ConfigHelper().EmployeeLimite += context.Request["code"] + ";";
                        else
                        {
                            var str = "";
                            var res = from a in userCode where a != context.Request["code"] select a;
                            foreach (string s in res)
                            {
                                str += s + ";";
                            }
                            new ConfigHelper().EmployeeLimite = str;
                        }
                        return;
                }
                if (serializer != null)
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        serializer.WriteObject(ms, data);
                        context.Response.Write(Encoding.UTF8.GetString(ms.ToArray()));
                        context.Response.End();
                    }
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}